package com.cangchu;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

/**
 * 自定义应用类，预留自定义设置
 */
public class MyApplication extends Application {

    private SharedPreferences sp;
    private SharedPreferences.Editor spEditor;
    @Override
    public void onCreate() {
        super.onCreate();
        sp = getSharedPreferences(Constant.SP_NAME, Context.MODE_PRIVATE);
    }


    public String getSpValue(String key){
        if(sp == null){
            sp = getSharedPreferences(Constant.SP_NAME, Context.MODE_PRIVATE);
        }
        return sp.getString(key, "");
    }

    public boolean setSpValue(String key,String value){
        if(sp == null){
            sp = getSharedPreferences(Constant.SP_NAME, Context.MODE_PRIVATE);
        }
        spEditor = sp.edit();
        spEditor.putString(key, value);
        return spEditor.commit();
    }

    public void setSpValue(Map<String,String> map){
        if(sp == null){
            sp = getSharedPreferences(Constant.SP_NAME, Context.MODE_PRIVATE);
        }
        spEditor = sp.edit();
        for(String key : map.keySet()){
            spEditor.putString(key, map.get(key));
        }
        spEditor.commit();
    }

    public SharedPreferences getSp() {
        return sp;
    }

    public SharedPreferences.Editor getSpEditor() {
        return spEditor;
    }
}
